package org.example.completed.controller;

import org.example.completed.domain.AdoptionApplication;
import org.example.completed.domain.AdoptionRecord;
import org.example.completed.domain.Pets;
import org.example.completed.domain.User;
import org.example.completed.model.vo.ResultVO;
import org.example.completed.service.AdoptionRecordService;
import org.example.completed.service.PetService;
import org.example.completed.service.UserService;
import org.example.completed.utils.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  领养记录 AdoptionRecordController
 *
 * @since 2025-04-07
 */
@RestController
@RequestMapping("/adoptionRecord")
public class AdoptionRecordController {
    @Autowired
    private AdoptionRecordService adoptionRecordService;
    @Autowired
    private UserService userService;
    @Autowired
    private PetService petService;

    /**
     * 获取所有领养记录列表
     * @return 领养记录列表
     */
    @GetMapping("/list")
    public ResultVO<List<AdoptionRecord>> getAllRecords() {
        List<AdoptionRecord> records = adoptionRecordService.list();
        return ResultVO.ok(records);
    }

    /**
     * 添加领养记录
     * @param record 领养记录实体
     * @return 操作结果
     */
    @Transactional
    @PostMapping("/add")
    public ResultVO<?> addRecord(@RequestBody AdoptionRecord record) {
        String userId = record.getUserId();
        String petId = record.getPetId();
        // 检查userId和petId是否存在
        try {
            // 检查userId是否存在
            User user = userService.getById(userId);
            if (user == null){
                return ResultVO.fail("用户不存在");
            }

            // 获取宠物信息
            Pets pet = petService.getById(petId);
            if (pet == null) {
                return ResultVO.fail("未找到对应宠物信息");
            }
            // 保存领养申请
            AdoptionRecord adoptionRecord = BeanCopyUtil.copyObject(record, AdoptionRecord.class);
            adoptionRecord.setAvatarUrl(user.getImg());
            adoptionRecord.setUserAcoount(user.getUserAcoount());
            adoptionRecord.setPetName(pet.getName());

            adoptionRecordService.save(adoptionRecord);
            return ResultVO.ok();
        } catch (Exception e) {
            return ResultVO.fail("数据库操作出错：" + e.getMessage());
        }
    }

    /**
     * 根据用户ID查询领养记录
     * @param userId 用户ID
     * @return 领养记录列表
     */
    @GetMapping("/getByUserId")
    public ResultVO<List<AdoptionRecord>> getRecordsByUserId(@RequestParam String userId) {
        List<AdoptionRecord> records = adoptionRecordService.getRecordsByUserId(userId);
        return ResultVO.ok(records);
    }

    /**
     * 根据宠物ID查询领养记录
     * @param petId 宠物ID
     * @return 领养记录列表
     */
    @GetMapping("/getByPetId")
    public ResultVO<List<AdoptionRecord>> getRecordsByPetId(@RequestParam String petId) {
        List<AdoptionRecord> records = adoptionRecordService.getRecordsByPetId(petId);
        return ResultVO.ok(records);
    }
}